from .models import CourseCategory, Course, Teacher, CourseChapter, CourseSection
from rest_framework import serializers


class CourseCategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = CourseCategory
        fields = ['id', 'name']


class TeacherSerializer(serializers.ModelSerializer):
    class Meta:
        model = Teacher
        fields = ['id', 'name', 'role', 'title', 'signature', 'image', 'brief']


class CourseSerializer(serializers.ModelSerializer):  # 课程列表和课程详情都用这个序列化类，序列化的字段多了好几个
    # 方式三：子序列化：通过老师的序列化类来实现序列化
    teacher = TeacherSerializer()

    class Meta:
        model = Course
        fields = [
            'id',
            'name',
            'course_img',
            'price',  # 课程价格
            'students',  # 学生数量
            'pub_sections',  # 发布了多少课时
            'sections',  # 总课时数量

            'brief',  # 课程介绍
            'attachment_path',  # 课程课件地址
            'period',  # 建议学习周期

            'course_type_name',  # 课程类型名字，表中没有这个字段，表模型中重写方法
            'level_name',  # 级别名字
            'status_name',  # 状态名字

            # 'teacher_detail',  # 老师  {name:xx,title:xxx}
            'teacher',
            'section_list',  # 课时 最多4个课时 [{},{},{},{}]
        ]


class CourseSectionSerializer(serializers.ModelSerializer):
    class Meta:
        model = CourseSection
        fields = ['name', 'orders', 'section_link', 'free_trail']


class CourseChapterSerializer(serializers.ModelSerializer):
    coursesections = CourseSectionSerializer(many=True)

    class Meta:
        model = CourseChapter
        fields = ['id', 'name', 'chapter', 'summary', 'coursesections']

    # coursesections = serializers.SerializerMethodField()

    # def get_coursesections(self, obj):
    #     return {'name': obj.CourseSection.name, 'orders': obj.CourseSection.orders,
    #             'section_link': obj.CourseSection.section_link, 'free_trail': obj.CourseSection.free_trail}
